from flask import Flask, request, jsonify
import os
from openai import OpenAI

app = Flask(__name__)

client = OpenAI(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key="sk-374d28568f82479b89cdbaa74cac4c06",  
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)

@app.route('/ask', methods=['GET'])
def ask_question():
    question = request.args.get('question')
    if not question:
        return jsonify({"error": "缺少问题参数"}), 400

    try:
        completion = client.chat.completions.create(
            model="deepseek-r1",  
            messages=[
                {'role': 'user', 'content': question}
            ]
        )

        result = {
            "answer": completion.choices[0].message.content
        }
        if hasattr(completion.choices[0].message, 'reasoning_content'):
            result["reasoning"] = completion.choices[0].message.reasoning_content

        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True, port=5001)